package com.woniuxy.java106crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.woniuxy.java106crm.entity.Plan;
import com.woniuxy.java106crm.mapper.PlanMapper;
import com.woniuxy.java106crm.request.ResPage;
import com.woniuxy.java106crm.service.MarketingService;
import com.woniuxy.java106crm.service.PlanService;
import com.woniuxy.java106crm.vo.marketing.LeadsPlanVo;
import com.woniuxy.java106crm.vo.marketing.PlanVo;
import com.woniuxy.java106crm.vo.marketing.StartVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class PlanServiceImpl implements PlanService {
    @Resource
    private PlanMapper planMapper;

    @Resource
    private MarketingService marketingService;

    @Override
    public boolean addStartPlan(PlanVo planVo) {
        // 查询指派时间
        String dateTimeString = marketingService.selectByAppointTime(planVo.getLid());
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 解析字符串为LocalDateTime
        LocalDate date = LocalDate.parse(dateTimeString, dateTimeFormatter);

        // 实体类设置日期
        planVo.setAppointTime(date);


        return planMapper.addStartPlan(planVo);
    }

    public List<StartVo> selectPlan(String uname) {
        return planMapper.selectPlan(uname);
    }

    @Override
    public ResPage<List<Plan>> selectAll(Integer page, Integer size, PlanVo planVo) {
        PageHelper.startPage(page, size);
        //
        List<Plan> planList = planMapper.selectAll(planVo);
        // 获取分页信息
        PageInfo<Plan> pageInfo = new PageInfo<>(planList);
        //
        ResPage<List<Plan>> resPage = new ResPage<>();
        resPage.setCurPage(pageInfo.getPageNum());
        resPage.setSize(pageInfo.getPageSize());
        resPage.setTotal(pageInfo.getTotal());
        resPage.setTotalPage(pageInfo.getPages());
        resPage.setData(pageInfo.getList());
        return resPage;
    }

    @Override
    public Plan findById(int id) {
        return planMapper.findById(id);
    }

    @Override
    public boolean updateById(Plan plan) {
        return planMapper.updateById(plan);
    }

    @Override
    public LeadsPlanVo selectLP(int id) {
        return planMapper.selectLP(id);
    }

    @Override
    public boolean addAllPlan(Plan plan) {
        return planMapper.addAllPlan(plan);
    }

    @Override
    public boolean delPlanById(int id) {
        return planMapper.delPlanById(id);
    }
}
